var loaderIndex = 0;
jQuery(document).ready(function() {
	jQuery.ajax({
		  type: "POST",
		  dataType: 'json',
		  url:"type/list",
		  success: function(data){
		  	 var optionstring = "";  
             for (var j = 0; j < data.length; j++) {  
                 optionstring += "<option value=\"" + data[j].id + "\" >" + data[j].name + "</option>";  
             }  
		  	jQuery("#cars").html("<option value='请选择'>请选择...</option> "+optionstring);
		  }
	});
	
	loaderData(0,loaderIndex);
	
});

/**
 * 
 * @param initIndex   判断是否是首次加载
 * @param loaderIndex 加载次数 分页使用
 */
function loaderData(initIndex,loaderIndex){
	
	
	
	jQuery.ajax({
		type: "POST",
		data:{currentPage:loaderIndex,limit:10},
		dataType: 'json',
		url: "article/list",
		success: function(data){
			var str ='';
			for (var j = 0; j < data.aaData.length; j++) { 
				var tmpText = data.aaData[j].text;
				str += '<div class="content"><a href="article/pre/'+data.aaData[j].id+'"><h2>'+data.aaData[j].title+'</h2></a><span class="flag">'+data.aaData[j].typeName+'</span>'+
	                '<div class="text-class-test" style= "text-overflow : clip;">'+tmpText+'</div>'+
	                '<span>发布于'+FormatDate(data.aaData[j].createTime)+'</span></div>';
			}
			if(initIndex==0){				
				jQuery("#myContent").html(str);
			}else{
				jQuery("#myContent").append(str);
			}
		}
	});
	
}

function search(){
	alert("就猜你会点~");
}

function selectChange(){
	
}

function add(){
	window.location.href ="article/pre";
}

function FormatDate (strTime) {
    var date = new Date(strTime);
    return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate() +" "+date.getHours()+":"+date.getMinutes() +":"+date.getSeconds();
}
(function(){
	setTimeout(function(){
		//console.log(jQuery('body').height());
		var innerHeight = jQuery(window).innerHeight();
		jQuery(document).on("scroll",function(){
			// 获取body总高度
			var totalHeight = jQuery('body').height();
			// 触发刷新高度
			var freshHeight = jQuery('body').scrollTop() + innerHeight;
			if(freshHeight == totalHeight){
				console.log("刷新");
				loaderIndex ++;
				loaderData(1,loaderIndex);
			}
			
		});
		},1000);
})();